import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
/* Layout */
import Layout from '@/layout'
import store from '@/store'
import { Store } from 'vuex'
// 功能简述: 不同角色登录显示不同的功能模块，所以把路由拆分为 常量路由、异步路由和任意路由

// 常量路由：任意角色登录都会显示的路由（登录、404、首页）
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }
]

// 异步路由：不同角色登录显示不同的路由
export const asyncRoutes = [
  {
    path: '/acl',
    name: 'Acl',
    component: Layout,
    meta: { title: '权限管理', icon: 'password' },
    children: [
      {
        path: '/user',
        name: 'User',
        component: () => import('@/views/acl/user/index'),
        meta: { title: '用户管理', icon: 'user' }
      },
      {
        path: '/role',
        name: 'Role',
        component: () => import('@/views/acl/Role/index'),
        meta: { title: '角色管理', icon: 'role' }
      },
      {
        path: '/permission',
        name: 'Permission',
        component: () => import('@/views/acl/Permission/index'),
        meta: { title: '菜单管理', icon: 'menu' }
      },
      //重定向，项目跑起来时，访问/acl，会定位到/acl/user组件
      {
        path: '/acl',
        redirect: "/user",
        component: () => import('@/views/acl/user/index'),
      }
    ]
  },
  {
    path: '/product',
    name: 'Product',
    component: Layout,
    meta: { title: '商品管理', icon: 'product' },
    children: [
      {
        path: '/trademark',
        name: "Trademark",
        component: () => import('@/views/product/Trademark/index'),
        meta: { title: '品牌管理', icon: 'trademark' }
      },
      {
        path: '/attr',
        name: 'Attr',
        component: () => import('@/views/product/Attr/index'),
        meta: { title: '平台属性', icon: 'attr' }
      },
      {
        path: '/spu',
        name: 'Spu',
        component: () => import('@/views/product/Spu/index'),
        meta: { title: 'Spu管理', icon: 'spu' }
      },
      {
        path: '/sku',
        name: 'Sku',
        component: () => import('@/views/product/Sku/index'),
        meta: { title: 'Sku管理', icon: 'sku' }
      },
      //重定向，项目跑起来时，访问/product，会定位到/product/trademark组件
      {
        path: '/product',
        redirect: "/trademark",
        component: () => import('@/views/product/Trademark/index'),
      }
    ]
  }
]

// 任意路由：重定向路由 / 重定向到404页面
export const anyRoutes = { path: '*', redirect: '/404', hidden: true }

// const allRoutes = store.state.user.resultAllRoutes

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [...constantRoutes, ...asyncRoutes, anyRoutes] // 防止刷新页面出现白屏，在此处也暴露一次路由，但控制台会报警告
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
